import {
  listModule
} from '@/api/module'
import {
  listDimension
} from '@/api/dimension'
import {
  getModules,
  setModules,
  getDimension,
  setDimension
} from '@/utils/auth.js'

const modules = {
  state: {
    modlist: getModules(),
    dimension: getDimension()
  },

  mutations: {
    SET_MOD: (state, mods) => {
      state.modlist = mods
    },
    SET_DIM: (state, dim) => {
      state.dimension = dim
    }
  },
  actions: {

    // 登录
    ListModules({
      commit
    }) {
      return new Promise((resolve, reject) => {
        listModule().then(response => {
          if (response.Result === 0) {
            commit('SET_MOD', response.Data)
            setModules(response.Data)
          } else {
            commit('SET_MOD', [])
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getDimension({
      commit
    }) {
      return new Promise((resolve, reject) => {
        listDimension().then(response => {
          if (response.Result === 0) {
            commit('SET_DIM', response.Data)
            setDimension(response.Data)
          } else {
            commit('SET_DIM', [])
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default modules
